﻿using ByteSite.API.Model;
using ByteSite.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using Yitter.IdGenerator;

namespace ByteSite.API.Controllers
{
    [RoutePrefix("api/invoice")]
    public class InvoiceController : ApiController
    {
        protected internal Entitys.sysconfig sysConfig;
        public InvoiceController()
        {
            sysConfig = new BLL.sysconfig().loadConfig();
        }

        [HttpPost]
        [Route("list")]
        public HttpResponseMessage list()
        {
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
            HttpRequestBase request = context.Request;//定义传统request对象

            InvoiceListRequest req = DTRequest.RequestFormParams<InvoiceListRequest>(request);
            LoggerHelper.Debug(JsonHelper2.ObjectToJSON(req));
            BaseResponse resp = new BaseResponse();
            if (string.IsNullOrEmpty(req.order_ids))
            {
                resp.success = false;
                resp.desc = "order_ids is null";
                var result_err = new HttpResponseMessage()
                {
                    Content = new StringContent(JsonHelper2.ObjectToJSON(resp), Encoding.UTF8, "application/json")
                };
                return result_err;
            }
            string[] order_ids = Utils.SplitString(req.order_ids, ",");
            string _order_ids = "";
            foreach (string order_id in order_ids)
            {
                _order_ids += "'"+order_id + "',";
            }
            _order_ids = _order_ids.TrimEnd(',');


            BLL.orders bll = new BLL.orders();
            List<Entitys.dt_orders> orders = bll.GetList(10000, "order_no in (" + _order_ids + ")", "id asc");

            Dictionary<string, List<string>> list = new Dictionary<string, List<string>>();
            foreach (Entitys.dt_orders o in orders)
            {
                list.Add(o.order_no, new List<string> { sysConfig.weburl + "/invoice/" + o.order_no + ".zip" });
            }

            resp.success = true;
            resp.result = list;

            LoggerHelper.Debug(JsonHelper2.ObjectToJSON(resp));
            var result = new HttpResponseMessage()
            {
                Content = new StringContent(JsonHelper2.ObjectToJSON(resp), Encoding.UTF8, "application/json")
            };
            return result;
        }



    }
}